import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class test5 {
    //力扣 15 三数之和
    public static void main(String[] args) {
        int[] nums = {-2,0,1,1,2};
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> list = new ArrayList<>();
        for (int max = n - 1; max >= 2; ) {

           if(nums[max]<0){
               break;
           }

            int left = 0;
            int right = max - 1;
            int target = -nums[max];

            while (left < right) {
                int sum = nums[left] + nums[right];
                if (sum > target) {
                    right--;

                } else if (sum < target) {
                    left++;

                } else {
                    List<Integer> newlist = new ArrayList<>();
                    newlist.add(nums[left]);
                    newlist.add(nums[right]);
                    newlist.add(nums[max]);
                    list.add(newlist);
//                    list.add(new ArrayList<Integer>(Arrays.asList(nums[left], nums[right], nums[max])));

                    left++; right--;
                    while (left < right && nums[left - 1] == nums[left]   ) {
                        left++;
                    }

                    while (left < right && nums[right + 1] == nums[right]  ) {
                        right--;
                    }
                }


            }
            max--;
            while (max >= 2 && nums[max + 1] == nums[max]  ) {
                max--;
            }
        }

        for (int i1 = 0; i1 < list.size(); i1++) {

            for (int i2 = 0; i2 < list.get(i1).size(); i2++) {
                System.out.print(list.get(i1).get(i2) + " ");
            }
        }

    }
}